package gui;

import java.io.*;
import java.lang.*;

public class doLZ77_compress {

    public String doLZ77(String inputstring) {
        String output = "";
        int CodingPosition;
        int windowsize = 255;
        System.out.println(inputstring.length());
        for (CodingPosition = 0; CodingPosition < inputstring.length(); CodingPosition++) {
            int maxmatch = 0;
            int matchindex = 0;
            int outchar = 0;
            if (CodingPosition < windowsize) {
                int j;
                int matchsize = 0;
                for (j = 0; j < CodingPosition; j++) {
                    while (CodingPosition + matchsize + 1 < inputstring.length() && j + matchsize < CodingPosition && matchsize < 255 && inputstring.substring(j, j + matchsize + 1).equals(inputstring.substring(CodingPosition, CodingPosition + matchsize + 1))) {
                        matchsize++;
                    }
                    if (matchsize > maxmatch) {
                        matchindex = j;
                        maxmatch = matchsize;
                    }
                    matchsize = 0;
                }
                if (maxmatch == 0) {
                    matchindex = 0;
                    outchar = (int) inputstring.charAt(CodingPosition);
                } else {
                    matchindex = CodingPosition - matchindex;
                    outchar = (int) inputstring.charAt(CodingPosition + maxmatch);
                    CodingPosition = CodingPosition + maxmatch;
                }

            } else {
                int j;
                int matchsize = 0;
                for (j = CodingPosition - windowsize; j < CodingPosition; j++) {
                    while (CodingPosition + matchsize + 1 < inputstring.length() && j + matchsize < CodingPosition && matchsize < 255 && inputstring.substring(j, j + matchsize + 1).equals(inputstring.substring(CodingPosition, CodingPosition + matchsize + 1))) {
                        matchsize++;
                    }
                    if (matchsize > maxmatch) {
                        matchindex = j;
                        maxmatch = matchsize;
                    }
                    matchsize = 0;
                }
                if (maxmatch == 0) {
                    matchindex = 0;
                    outchar = (int) inputstring.charAt(CodingPosition);
                } else {
                    matchindex = CodingPosition - matchindex;
                    outchar = (int) inputstring.charAt(CodingPosition + maxmatch);
                    CodingPosition = CodingPosition + maxmatch;
                }
            }

            output = output + (char) matchindex + (char) maxmatch + (char) outchar;
        }
        return output;
    }

    public void doCompress(String InputFile, String OutputFile) {
        //OutputFile=OutputFile+".raa";
        try {
            File inputfile = new File(InputFile);
            FileInputStream fileinstream = new FileInputStream(inputfile);
            String inputstring = "";
            int i;
            long length = inputfile.length();
            for (i = 0; i < length; i++) {
                inputstring = inputstring + (char) fileinstream.read();
            }
            fileinstream.close();
            String output = doLZ77(inputstring);
            System.out.println(output.length());
            File outputfile = new File(OutputFile);
            FileOutputStream fileoutstream = new FileOutputStream(outputfile);
            DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
            dataoutstream.writeBytes(output);
            dataoutstream.close();
        } catch (Exception e) {
        }
    }
    /*        public static void main(String args[])
    {
    doCompress(args);
    } */
}